//给定整数数组 nums 和整数 k，请返回数组中第 k 个最大的元素。 
//
// 请注意，你需要找的是数组排序后的第 k 个最大的元素，而不是第 k 个不同的元素。 
//
// 
//
// 示例 1: 
//
// 
//输入: [3,2,1,5,6,4] 和 k = 2
//输出: 5
// 
//
// 示例 2: 
//
// 
//输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
//输出: 4 
//
// 
//
// 提示： 
//
// 
// 1 <= k <= nums.length <= 10⁴ 
// -10⁴ <= nums[i] <= 10⁴ 
// 
// Related Topics 数组 分治 快速选择 排序 堆（优先队列） 👍 1550 👎 0

package leetcode.editor.cn;

import java.util.Arrays;
import java.util.PriorityQueue;

public class _215_KthLargestElementInAnArray {
    public static void main(String[] args) {
        Solution solution = new _215_KthLargestElementInAnArray().new Solution();
        int[] nums = new int[]{3, 2, 3, 1, 2, 4, 5, 5, 6};
        int k = 4;
        System.out.println(solution.findKthLargest(nums, k));

        System.out.println(Arrays.toString(nums));
        solution.heapSort(nums);
        System.out.println(Arrays.toString(nums));
    }

    class Solution {
        public int findKthLargest(int[] nums, int k) {
            System.out.println((0 - 1) / 2);
            return -1;
        }

        // 实现堆排序

        private void heapSort(int[] nums) {
            if (nums == null || nums.length <= 1) return;
            int heapSize = 1;
            for (int i = 1; i < nums.length; i++) {
                heapInsert(nums, i);
            }
            heapSize = nums.length;
            while (heapSize > 0) {
                swap(nums, 0, heapSize - 1);
                heapfiy(nums, 0, --heapSize);
            }
        }

        private void heapfiy(int[] nums, int index, int heapSize) {
            int left = 2 * index + 1;
            while (left < heapSize) {
                int largestIndex = left + 1 < heapSize && nums[left + 1] > nums[left] ? left + 1 : left;
                largestIndex = nums[largestIndex] > nums[index] ? largestIndex : index;
                if (largestIndex == index) {
                    break;
                }
                swap(nums, largestIndex, index);
                index = largestIndex;
                left = 2 * index + 1;
            }
        }

        private void heapInsert(int[] nums, int index) {
            while (nums[index] > nums[(index - 1) / 2]) {
                swap(nums, index, (index - 1) / 2);
                index = (index - 1) / 2;
            }
        }

        private void swap(int[] nums, int a, int b) {
            int tmp = nums[a];
            nums[a] = nums[b];
            nums[b] = tmp;
        }

    }

    class Solution2 {
        public int findKthLargest(int[] nums, int k) {
            PriorityQueue<Integer> heap = new PriorityQueue<Integer>((n1, n2) -> n1 - n2);
            for (int n : nums) {
                heap.add(n);
                if (heap.size() > k) {
                    heap.poll();
                }
            }
            return heap.poll();
        }
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    // 自己的方法，太草率了
    class Solution1 {
        public int findKthLargest(int[] nums, int k) {
            Arrays.sort(nums);
            return nums[nums.length - k];
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}